<template>
  <h1>首页</h1>
</template>

<script>
import request from "@/utils/request";
import { inject } from "vue";
import router from "@/router";

export default {
  // eslint-disable-next-line vue/multi-word-component-names
  name: "mHome",
  data() {
    return {
      user: {},
      msg: "",
      userCode: "",
      appid: "wx1ef8aab656db05b8",
      appsecret: "46a70f317184bf347a8e5a8e375011f8",
      openId: "",
      userInformation : {}
    };
  },
  mounted() {
    this.getUser();
  },

  methods: {
    // 获取用户信息
    getUser() {
      this.userCode = this.getUrlKey("code");
      this.getOpenId(this.userCode);
    },
    // 获取openId
    getOpenId(code) {
      if (code) {
        request.get("/restGet/restTemplateTestGet/" + code).then(res => {
          if (res.data.code === "200") {
            this.openId = res.data.data.openid;
            this.getUserInfo();
          }
        });
      } else {
        // 无code->跳转login
        // console.log("无code");
        // location.replace("/login")
      }
    },
    // 通过openId获取账户信息
    getUserInfo: function() {
      request.post("/account/wxLogin", this.openId).then(res => {
        if (res.data.code === "200") {
          localStorage.setItem("userInformation",JSON.stringify(res.data.data))
          this.userInformation = res.data.data
        }
      });
    },
    // 获取url中的code
    getUrlKey(name) {
      //获取url 参数
      return (
        decodeURIComponent(
          (new RegExp("[?|&]" + name + "=" + "([^&;]+?)(&|#|;|$)").exec(
            location.href
          ) || [, ""])[1].replace(/\+/g, "%20")
        ) || null
      );
    }
  }
};
</script>

<style scoped></style>
